/**
 * 租户管理相关类型定义
 */

// 租户基本信息
export interface Tenant {
  id: string
  name: string
  displayName: string
  description?: string
  isActive: boolean
  isDefault: boolean
  editionId?: string
  editionDisplayName?: string
  connectionString?: string
  keycloakRealm?: string
  keycloakClientId?: string
  creationTime: string
  lastModificationTime?: string
  creatorId?: string
  lastModifierId?: string
}

// 租户创建请求
export interface CreateTenantRequest {
  name: string
  displayName: string
  description?: string
  editionId?: string
  connectionString?: string
  adminEmailAddress: string
  adminPassword: string
  keycloakRealm?: string
  keycloakClientId?: string
  keycloakClientSecret?: string
  features?: Record<string, any>
  settings?: Record<string, string>
}

// 租户更新请求
export interface UpdateTenantRequest {
  name: string
  displayName: string
  description?: string
  isActive: boolean
  editionId?: string
  connectionString?: string
  keycloakRealm?: string
  keycloakClientId?: string
  keycloakClientSecret?: string
  concurrencyStamp: string
}

// 租户查询参数
export interface TenantQueryParams {
  filter?: string
  isActive?: boolean
  isDefault?: boolean
  editionId?: string
  sorting?: string
  skipCount?: number
  maxResultCount?: number
}

// 租户查询结果
export interface TenantQueryResult {
  items: Tenant[]
  totalCount: number
}

// 租户统计信息
export interface TenantStatistics {
  totalTenants: number
  activeTenants: number
  inactiveTenants: number
  defaultTenant: number
  totalUsers: number
  totalEditions: number
  recentTenants: Tenant[]
}

// 租户连接字符串信息
export interface TenantConnectionString {
  name: string
  value: string
  isDefault: boolean
}

// Keycloak Realm信息
export interface KeycloakRealmInfo {
  realm: string
  displayName?: string
  enabled: boolean
  registrationAllowed: boolean
  rememberMe: boolean
  loginWithEmailAllowed: boolean
  duplicateEmailsAllowed: boolean
  verifyEmail: boolean
  resetPasswordAllowed: boolean
  editUsernameAllowed: boolean
  bruteForceProtected: boolean
  permanentLockout: boolean
  maxFailureBeforeTemporaryLockout: number
  waitIncrementSeconds: number
  minimumQuickLoginWaitSeconds: number
  maxDeltaTimeSeconds: number
  failureFactor: number
  sslRequired: string
  registrationFlow?: string[]
  loginTheme?: string
  internationalizationEnabled: boolean
  supportedLocales: string[]
  defaultLocale: string
  accountTheme?: string
  adminTheme?: string
  emailTheme?: string
}

// Keycloak客户端信息
export interface KeycloakClientInfo {
  clientId: string
  name?: string
  description?: string
  enabled: boolean
  clientAuthenticatorType: string
  secret?: string
  registrationAccessToken?: string
  defaultRoles?: string[]
  redirectUris?: string[]
  webOrigins?: string[]
  notBefore?: number
  bearerOnly: boolean
  consentRequired: boolean
  standardFlowEnabled: boolean
  implicitFlowEnabled: boolean
  directAccessGrantsEnabled: boolean
  serviceAccountsEnabled: boolean
  publicClient: boolean
  frontchannelLogout: boolean
  protocol: string
  attributes: Record<string, string>
  authenticationFlowBindingOverrides?: Record<string, string>
  fullScopeAllowed: boolean
  nodeReRegistrationTimeout: number
  defaultClientScopes?: string[]
  optionalClientScopes?: string[]
}

// 租户功能设置
export interface TenantFeature {
  name: string
  displayName?: string
  description?: string
  defaultValue: any
  valueType: 'FreeText' | 'ToggleString' | 'Selection'
  allowedValues?: any[]
  isVisible: boolean
  isDisabled: boolean
}

// 租户设置
export interface TenantSetting {
  name: string
  displayName?: string
  description?: string
  defaultValue: string
  isVisible: boolean
  isDisabled: boolean
  group: string
}

// 租户操作记录
export interface TenantOperationLog {
  id: string
  tenantId: string
  tenantName: string
  operation: string
  operationTime: string
  operatorId?: string
  operatorName?: string
  details?: string
  ipAddress?: string
  userAgent?: string
}

// 租户管理员信息
export interface TenantAdmin {
  id: string
  userName: string
  email: string
  name?: string
  surname?: string
  isActive: boolean
  lockoutEnabled: boolean
  lockoutEnd?: string
  twoFactorEnabled: boolean
  phoneNumber?: string
  phoneNumberVerified: boolean
  creationTime: string
  lastModificationTime?: string
  lastLoginTime?: string
}

// 租户用户统计
export interface TenantUserStatistics {
  totalUsers: number
  activeUsers: number
  inactiveUsers: number
  lockedUsers: number
  twoFactorEnabledUsers: number
  recentRegistrations: number
  recentLogins: number
}

// 租户状态枚举
export enum TenantStatus {
  Active = 'Active',
  Inactive = 'Inactive',
  Suspended = 'Suspended',
  Pending = 'Pending'
}

// 租户类型枚举
export enum TenantType {
  Default = 'Default',
  Standard = 'Standard',
  Enterprise = 'Enterprise',
  Trial = 'Trial'
}

// 租户导入请求
export interface ImportTenantRequest {
  file: File
  overwriteExisting: boolean
  sendWelcomeEmail: boolean
}

// 租户导出请求
export interface ExportTenantRequest {
  tenantIds: string[]
  format: 'Json' | 'Excel' | 'Csv'
  includeUsers: boolean
  includeSettings: boolean
  includeFeatures: boolean
}

// 租户批量操作请求
export interface BulkTenantOperationRequest {
  tenantIds: string[]
  operation: 'Activate' | 'Deactivate' | 'Delete' | 'UpdateEdition'
  parameters?: Record<string, any>
}

// 租户连接字符串验证请求
export interface ValidateConnectionStringRequest {
  tenantId?: string
  connectionString: string
  provider: string
}

// 租户连接字符串验证结果
export interface ValidateConnectionStringResult {
  isValid: boolean
  errorMessage?: string
  databaseVersion?: string
  availableTables?: string[]
}

// 租户健康检查结果
export interface TenantHealthCheckResult {
  tenantId: string
  tenantName: string
  status: 'Healthy' | 'Unhealthy' | 'Degraded'
  checks: {
    database: {
      isHealthy: boolean
      responseTime?: number
      errorMessage?: string
    }
    keycloak: {
      isHealthy: boolean
      responseTime?: number
      errorMessage?: string
    }
    features: {
      isHealthy: boolean
      issues?: string[]
    }
    settings: {
      isHealthy: boolean
      issues?: string[]
    }
  }
  lastCheckTime: string
}

// 租户备份信息
export interface TenantBackup {
  id: string
  tenantId: string
  tenantName: string
  fileName: string
  fileSize: number
  createdTime: string
  creatorId?: string
  creatorName?: string
  type: 'Full' | 'Incremental' | 'Differential'
  status: 'In Progress' | 'Completed' | 'Failed'
  downloadUrl?: string
  retentionDays: number
}

// 租户备份请求
export interface CreateTenantBackupRequest {
  tenantId: string
  type: 'Full' | 'Incremental' | 'Differential'
  includeUsers: boolean
  includeSettings: boolean
  includeFeatures: boolean
  retentionDays?: number
  description?: string
}

// 租户恢复请求
export interface RestoreTenantBackupRequest {
  backupId: string
  overwriteExisting: boolean
  restoreUsers: boolean
  restoreSettings: boolean
  restoreFeatures: boolean
  targetTenantId?: string
}

// 租户活动日志
export interface TenantActivityLog {
  id: string
  tenantId: string
  tenantName: string
  activity: string
  activityTime: string
  userId?: string
  userName?: string
  details?: string
  ipAddress?: string
  userAgent?: string
  category: string
  level: 'Information' | 'Warning' | 'Error'
}